import 'package:flutter/material.dart';
import 'package:lite_message/style/lm_font_style.dart';
import 'package:lite_message/style/lm_icons.dart';
import 'package:lite_message/util/data_util.dart';
import 'package:lite_message/util/dio_util.dart';
import 'package:lite_message/widgets/custom_bg.dart';
import 'package:lite_message/widgets/lm_button.dart';
import 'package:lite_message/widgets/lm_text_field.dart';
import 'package:lite_message/widgets/logo.dart';

import 'package:lite_message/util/app_util.dart';

///注册界面
class RegistPage extends StatelessWidget {
  const RegistPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomBg(
      body: ListView(
        physics: LM.LmPhysics,
        padding: EdgeInsets.symmetric(horizontal: 90.px),
        children: [
          SizedBox(height: 80.px),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              const Logo(sizeType: 2),
              SizedBox(width: 40.px),
              Text('注册', style: LMFontStyle.title2(context)),
            ],
          ),
          SizedBox(height: 100.px),
          const RegistBody(),
        ],
      ),
    );
  }
}

///输入框主体
class RegistBody extends StatefulWidget {
  const RegistBody({Key key}) : super(key: key);

  @override
  _RegistBodyState createState() => _RegistBodyState();
}

class _RegistBodyState extends State<RegistBody> {
  Map _data;

  FocusNode _focusNodeTel;
  FocusNode _focusNodeEmail;
  FocusNode _focusNodePsw;
  FocusNode _focusNodeConPsw;

  @override
  void initState() {
    super.initState();
    _data = Map<String, String>();
    _data['username'] = '';
    _data['tel'] = '';
    _data['email'] = '';
    _data['password'] = '';
    _data['confirmPassword'] = '';

    _focusNodeTel = FocusNode();
    _focusNodeEmail = FocusNode();
    _focusNodePsw = FocusNode();
    _focusNodeConPsw = FocusNode();
  }

  ///注册
  _regist() async {
    LM.log('data:$_data');

    if (_data['username'].toString().trim().length <= 0) {
      LM.log('昵称格式有误', type: 1);
      LM.t('昵称格式有误');
      return;
    }

    if (!DataUtil.isPhone(_data['tel'])) {
      LM.log('电话号码格式有误', type: 1);
      LM.t('电话号码格式有误');
      return;
    }

    if (!DataUtil.isEmail(_data['email'])) {
      LM.log('邮箱格式有误', type: 1);
      LM.t('邮箱格式有误');
      return;
    }

    if (!DataUtil.psw(_data['password'])) {
      LM.log('密码格式有误', type: 1);
      LM.t('密码格式有误');
      return;
    }

    if (_data['password'] != _data['confirmPassword']) {
      LM.log('两次密码不一致', type: 1);
      LM.t('两次密码不一致');
      return;
    }

    await DioUtil.regist(context, _data);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        LMTextField(
          hintText: '昵称',
          maxLength: 14,
          icon: LmIcons.user,
          onChanged: (v) => _data['username'] = v,
          textInputAction: TextInputAction.next,
          onEditingComplete: () =>
              FocusScope.of(context).requestFocus(_focusNodeTel),
        ),
        SizedBox(height: 60.px),
        LMTextField(
          focusNode: _focusNodeTel,
          hintText: '电话',
          maxLength: 11,
          keyboardType: TextInputType.phone,
          icon: LmIcons.phone,
          iconSize: 65.px,
          onChanged: (v) => _data['tel'] = v,
          textInputAction: TextInputAction.next,
          onEditingComplete: () =>
              FocusScope.of(context).requestFocus(_focusNodeEmail),
        ),
        SizedBox(height: 60.px),
        LMTextField(
          focusNode: _focusNodeEmail,
          hintText: '邮箱',
          keyboardType: TextInputType.emailAddress,
          icon: LmIcons.email,
          onChanged: (v) => _data['email'] = v,
          textInputAction: TextInputAction.next,
          onEditingComplete: () =>
              FocusScope.of(context).requestFocus(_focusNodePsw),
        ),
        SizedBox(height: 60.px),
        LMTextField(
          focusNode: _focusNodePsw,
          maxLength: 16,
          hintText: '密码(至少6位)',
          obscureText: true,
          keyboardType: TextInputType.text,
          icon: LmIcons.lock,
          onChanged: (v) => _data['password'] = v,
          textInputAction: TextInputAction.next,
          onEditingComplete: () =>
              FocusScope.of(context).requestFocus(_focusNodeConPsw),
        ),
        SizedBox(height: 60.px),
        LMTextField(
          focusNode: _focusNodeConPsw,
          maxLength: 16,
          obscureText: true,
          hintText: '重复密码',
          keyboardType: TextInputType.text,
          icon: LmIcons.lock,
          onChanged: (v) => _data['confirmPassword'] = v,
        ),
        SizedBox(height: 90.px),
        LmButton(
          text: '注册',
          onPressed: () async => await _regist(),
        ),
      ],
    );
  }
}
